/*
 * File Name：BookcaseGridVAdapter.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： BookcaseGridVAdapter.java
 * Modify By：res-dlzhou
 * Modify Date：2015年3月9日
 * Modify Type：Add
 */
package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子报适配器
 *
 * @author res-dlzhou
 * @version ciwong v.1.0 2015年3月9日
 * @since ciwong v.1.0
 */
public class BookcaseGridVAdapter extends BaseAdapter {
    private Activity activity;

    private List<EpaperInfo> epaperInfos;

    private int bWidth, bHeight, tHeight, space;

    public BookcaseGridVAdapter(Activity activity, List<EpaperInfo> epaperInfos,
                                int bookWidth, int bookHeight, int textHeight, int space) {
        this.activity = activity;
        this.epaperInfos = epaperInfos;
        this.bWidth = bookWidth;
        this.bHeight = bookHeight;
        this.tHeight = textHeight;
        this.space = space;
    }

    public List<EpaperInfo> getEpaperInfos() {
        return epaperInfos;
    }

    @Override
    public int getCount() {
        return epaperInfos == null ? 0 : epaperInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return epaperInfos == null ? null : epaperInfos.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = View.inflate(activity, R.layout.item_bookcase_gridv_layout,
                    null);
        } else {
            v = convertView;
        }
        ImageView bCaseIconIv = ViewHolder.get(v, R.id.item_bookcase_icon_iv);
        View bottomView = ViewHolder.get(v, R.id.item_book_bottom);
        View imgFreeView = ViewHolder.get(v, R.id.img_item_free);
        ImageView itemBookcaseClick = ViewHolder.get(v,
                R.id.item_bookcase_click);
        ImageView kaoShiView=ViewHolder.get(v, R.id.kao_shi);
        final EpaperInfo epaperInfo = epaperInfos.get(position);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) bCaseIconIv
                .getLayoutParams();

//        Log.d("layoutParams", "#####bWidth######"+bWidth);
//        Log.d("layoutParams", "#####bHeight######"+bHeight);

        params.width = bWidth;
        params.height = bHeight;
//        params.topMargin = (int) (space * 1.2f);
        bCaseIconIv.setLayoutParams(params);

        itemBookcaseClick.setLayoutParams(params);
        ImageLoader.getInstance().displayImage(epaperInfo.getCover(),
                new ImageViewAware(bCaseIconIv), EConstants.getBookCoverOption());

        imgFreeView.setVisibility(
                epaperInfo.getIsFree() == 1 ? View.VISIBLE : View.GONE);

        params = (RelativeLayout.LayoutParams) bottomView.getLayoutParams();
        params.width = bWidth;
        bottomView.setLayoutParams(params);
        //考试用书
        if(epaperInfo.getRequired()==1){
            kaoShiView.setVisibility(View.VISIBLE);
        }else{
            kaoShiView.setVisibility(View.GONE);
        }


        return v;
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.epaperInfos != null) {
            this.epaperInfos.clear();
            //notifyDataSetChanged();
        }
    }

    /**
     * 移除数据
     *
     * @param epaperInfo 书籍数据
     */
    public void remove(EpaperInfo epaperInfo) {
        if (this.epaperInfos != null) {
            this.epaperInfos.remove(epaperInfo);
        }
    }


    /**
     * 移除数据
     *
     * @param epaperInfoList 书籍数据集合
     */
    public void removeAll(List<EpaperInfo> epaperInfoList) {
        if (this.epaperInfos != null && epaperInfoList != null) {
            this.epaperInfos.removeAll(epaperInfoList);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据
     *
     * @param epaperInfo 书籍数据集合
     */
    public void add(EpaperInfo epaperInfo) {
        if (this.epaperInfos != null) {
            this.epaperInfos.add(epaperInfo);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据
     *
     * @param epaperInfos 书籍数据集合
     */
    public void addAll(List<EpaperInfo> epaperInfos) {
        if (this.epaperInfos != null) {
            this.epaperInfos.addAll(epaperInfos);
            notifyDataSetChanged();
        }
    }

    /**
     * 更新数据
     */
    public void updateAll(List<EpaperInfo> epaperInfos) {
        if (this.epaperInfos != null) {
            this.epaperInfos.clear();
            this.epaperInfos.addAll(epaperInfos);
        } else {
            this.epaperInfos=new ArrayList<EpaperInfo>();
            this.epaperInfos.addAll(epaperInfos);
        }
        notifyDataSetChanged();
    }
}
